package com.quiz.bis.controller;

import com.quiz.bis.domain.model.*;
import com.quiz.bis.domain.vo.QuestionDetailVo;
import com.quiz.bis.mapper.ExamMapper;
import com.quiz.bis.mapper.QuestionMapper;
import com.quiz.bis.mapper.WrongQuestionRecordMapper;
import com.quiz.bis.service.IWrongQuestionService;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 错题本控制器
 *
 * @author ahuan
 */
@Tag(name = "错题本管理")
@RestController
@RequestMapping("/wrongQuestion")
@RequiredArgsConstructor
public class WrongQuestionController {
    private final IWrongQuestionService wrongQuestionService;

    private final WrongQuestionRecordMapper recordMapper;
    private final QuestionMapper questionMapper;
    private final ExamMapper examMapper;


    // 错题本管理
    @Operation(summary = "分页查询错题本列表")
    @GetMapping("/book/page")
    public TableDataInfo<WrongQuestionBook> pageBookList(Long userId, PageQuery page) {
        return wrongQuestionService.pageBookList(userId, page);
    }

    @Operation(summary = "查询错题本列表")
    @GetMapping("/book/list")
    public R<List<WrongQuestionBook>> listBooks(Long userId) {
        return R.ok(wrongQuestionService.listBooks(userId));
    }

    @Operation(summary = "获取默认错题本")
    @GetMapping("/book/default")
    public R<WrongQuestionBook> getDefaultBook(Long userId) {
        return R.ok(wrongQuestionService.getDefaultBook(userId));
    }

    @Operation(summary = "创建错题本")
    @PostMapping("/book")
    public R<Long> createBook(@RequestBody WrongQuestionBook book) {
        return wrongQuestionService.createBook(book);
    }

    @Operation(summary = "更新错题本")
    @PutMapping("/book")
    public R<Void> updateBook(@RequestBody WrongQuestionBook book) {
        return wrongQuestionService.updateBook(book);
    }

    @Operation(summary = "删除错题本")
    @DeleteMapping("/book/{bookId}")
    public R<Void> deleteBook(@PathVariable Long bookId) {
        return wrongQuestionService.deleteBook(bookId);
    }

    @Operation(summary = "设置默认错题本")
    @PutMapping("/book/default/{bookId}")
    public R<Void> setDefaultBook(Long userId, @PathVariable Long bookId) {
        return wrongQuestionService.setDefaultBook(userId, bookId);
    }

    /** 分页查询错题记录
     *
     * @param bookId
     * @param isMastered
     * @param typeId
     * @param keyword
     * @param difficult
     * @param page
     * @return
     */
    @GetMapping("/record/page")
    public TableDataInfo<WrongQuestionRecord> pageRecordList(Long bookId,
                                                             Integer isMastered,
                                                             Integer typeId,
                                                             String keyword,
                                                             Integer difficult,
                                                             PageQuery page) {
        return wrongQuestionService.pageRecordList(bookId,
                isMastered,
                typeId,
                keyword,
                difficult,
                page);
    }

    @Operation(summary = "添加错题记录")
    @PostMapping("/record")
    public R<Void> addRecord(Long bookId, Long questionAnswerId) {
        return wrongQuestionService.addRecord(bookId, questionAnswerId);
    }

    @Operation(summary = "批量添加错题记录")
    @PostMapping("/record/batch")
    public R<Void> batchAddRecords(Long bookId, @RequestBody List<Long> questionAnswerIds) {
        return wrongQuestionService.batchAddRecords(bookId, questionAnswerIds);
    }

    @Operation(summary = "删除错题记录")
    @DeleteMapping("/record/{recordId}")
    public R<Void> removeRecord(@PathVariable Long recordId) {
        return wrongQuestionService.removeRecord(recordId);
    }

    @Operation(summary = "批量删除错题记录")
    @DeleteMapping("/record/batch")
    public R<Void> batchRemoveRecords(@RequestBody List<Long> recordIds) {
        return wrongQuestionService.batchRemoveRecords(recordIds);
    }

    @Operation(summary = "标记掌握状态")
    @PutMapping("/record/master/{recordId}")
    public R<Void> markMastered(@PathVariable Long recordId, Integer mastered) {
        return wrongQuestionService.markMastered(recordId, mastered);
    }

    @Operation(summary = "更新错题标签")
    @PutMapping("/record/tags/{recordId}")
    public R<Void> updateRecordTags(@PathVariable Long recordId, String tagsJson) {
        return wrongQuestionService.updateRecordTags(recordId, tagsJson);
    }

    // 错题练习
    @Operation(summary = "生成错题练习")
    @GetMapping("/practice/generate")
    public R<Map<String, Object>> generatePractice(Long bookId, Integer count, Integer strategy) {
        return wrongQuestionService.generatePractice(bookId, count, strategy);
    }

    @Operation(summary = "提交练习结果")
    @PostMapping("/practice/submit")
    public R<Void> submitPractice(Long recordId, String answer, Integer timeSpent) {
        return wrongQuestionService.submitPractice(recordId, answer, timeSpent);
    }

    // 错题统计
    @Operation(summary = "分页查询错题统计")
    @GetMapping("/stats/page")
    public TableDataInfo<WrongQuestionStats> pageStats(Long userId, Long bookId, PageQuery page) {
        return wrongQuestionService.pageStats(userId, bookId, page);
    }

    @Operation(summary = "获取概览统计")
    @GetMapping("/stats/overview")
    public R<Map<String, Object>> getOverviewStats(Long userId, Long bookId) {
        return wrongQuestionService.getOverviewStats(userId, bookId);
    }

    @Operation(summary = "获取知识点统计")
    @GetMapping("/stats/knowledge")
    public R<List<Map<String, Object>>> getKnowledgeStats(Long userId, Long bookId) {
        return wrongQuestionService.getKnowledgeStats(userId, bookId);
    }

    @Operation(summary = "获取题型统计")
    @GetMapping("/stats/type")
    public R<List<Map<String, Object>>> getTypeStats(Long userId, Long bookId) {
        return wrongQuestionService.getTypeStats(userId, bookId);
    }

    // 智能推荐
    @Operation(summary = "推荐相关题目")
    @GetMapping("/recommend")
    public R<List<QuestionDetailVo>> recommendQuestions(Long recordId, Integer count, Integer strategy) {
        return wrongQuestionService.recommendQuestions(recordId, count, strategy);
    }

    @Operation(summary = "获取错题记录详情")
    @GetMapping("/record/{recordId}")
    public R<WrongQuestionRecord> getRecordDetail(@PathVariable Long recordId) {
        WrongQuestionRecord record = recordMapper.selectById(recordId);
        if (record == null) {
            return R.fail("记录不存在");
        }

        // 关联题目信息
        record.setQuestion(questionMapper.selectQuestionById(record.getQuestionId()));

        // 关联考试信息
        if (record.getExamId() != null) {
            record.setExam(examMapper.selectById(record.getExamId()));
        }

        return R.ok(record);
    }



    @Operation(summary = "获取可同步的错题列表")
    @GetMapping("/wrongAnswers")
    public R<List<ExamQuestionAnswers>> getWrongAnswers(Long userId) {
        return wrongQuestionService.getWrongAnswers(userId);
    }

    @Operation(summary = "同步错题到错题本")
    @PostMapping("/sync")
    public R<Void> syncWrongAnswers(Long bookId, @RequestBody List<Long> answerIds) {
        return wrongQuestionService.syncWrongAnswers(bookId, answerIds);
    }

}